<?php
/**
 * 前台深度垂直控制器
 */

define('IS_MOBILE', true);
require(dirname(__FILE__) . '/../includes/init.php');
include_once(ROOT_PATH . '/includes/funTvd.php');
@ini_set('display_errors',        1);

/* 是否有id参数 */
if (empty($_REQUEST['id'])) {
    header('location: index.php');
    exit;
} else {
    $id = intval($_REQUEST['id']);
}

/* 初始化赋值模板 */
assign_template();
$smarty->assign('page_title',      htmlspecialchars($_CFG['shop_title'])); // 首页页面标题
$smarty->assign('keywords',        htmlspecialchars($_CFG['shop_keywords'])); // 首页SEO网站关键词
$smarty->assign('description',     htmlspecialchars($_CFG['shop_desc'])); // 首页SEO网站描述

/* 收藏文章 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'share') {
    $sql = "UPDATE " . $GLOBALS['ecs']->table('tvd') . " SET share = share + 1 WHERE tvd_id = '$id'";
            $GLOBALS['db']->query($sql);
    $result['error']   = 0;
    $result['message']   = '成功分享';
    echo JSON()->encode($result);
    exit();
}

/* 收藏文章 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'collect') {
    if (!empty($_SESSION['user_id'])) {
        $sql = "UPDATE " . $ecs->table('tvd') . " SET praise = praise + 1 WHERE tvd_id = '$id'";
        $db->query($sql);
        $sql = "SELECT count(*) AS iscollect FROM " . $GLOBALS['ecs']->table('tvd_collect') . " WHERE tvd_id = '$id' AND user_id = '" . $_SESSION['user_id'] . "'";
        $iscollect = $GLOBALS['db']->getOne($sql);
        if (empty($iscollect)) {
            $sql = "INSERT INTO " . $GLOBALS['ecs']->table('tvd_collect') . " (tvd_id, user_id) VALUES('$id', '$_SESSION[user_id]')";
            $GLOBALS['db']->query($sql);
            $result['error']   = 0;
            $result['message']   = '关注成功';
        } else {
            $result['error']   = 1;
            $result['message']   = '已关注';
        }
    } else {
            $result['error']   = 1;
            $result['message']   = '请登录后再关注';
    }
    echo JSON()->encode($result);
    exit();
}

/* 取消收藏 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'uncollect') {
    if (!empty($_SESSION['user_id'])) {
        $sql = "SELECT count(*) AS iscollect FROM " . $GLOBALS['ecs']->table('tvd_collect') . " WHERE tvd_id = '$id' AND user_id = '" . $_SESSION['user_id'] . "'";
        $iscollect = $GLOBALS['db']->getOne($sql);
        if (empty($iscollect)) {
            $result['error']   = 1;
            $result['message']   = '收藏信息出错';
        } else {
            $sql = "DELETE FROM " . $GLOBALS['ecs']->table('tvd_collect') . " WHERE tvd_id = '$id' AND user_id = '" . $_SESSION['user_id'] . "'";
            $GLOBALS['db']->query($sql);
            $result['error']   = 0;
        }
    } else {
            $result['error']   = 1;
            $result['message']   = '请登录后再取消收藏';
    }
    echo JSON()->encode($result);
    exit();
}

/* 评论文章 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'comment') {
    assign_template();
    if (!empty($_SESSION['user_id']) && !empty($_REQUEST['comment'])) {
        $comment = strip_tags($_REQUEST['comment']);
        $at_user = strip_tags(trim($_REQUEST['at_user']));
        $sql = "INSERT INTO egg_tvd_comment (tvd_id, user_id, user_name, content, at_user) VALUES('$id', '$_SESSION[user_id]', '$_SESSION[user_name]', '$comment', '$at_user')";
        $iscomment = $GLOBALS['db']->query($sql);
        $result['error']   = 0;
        $result['message']   = '评论成功';
        $smarty->assign('commentList', getTvdComment($id, 0, 1));// 内容信息
        $result['content']   = $smarty->fetch("tvd/comtList.lbi");
    } else {
            $result['error']   = 1;
            $result['message']   = '请登录后再评论';
    }
    echo JSON()->encode($result);
    exit();
}


/* 评论列表 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'commentList') {
    assign_template();
    $smarty->assign('id', $id);// ID
    /* AJAX获取评论列表 */
    if ($_REQUEST['isAjax'] == 1) {
        include_once('includes/cls_json.php');
        $json  = new JSON;
        $commentList = getTvdComment($id, 0, 100);
        $smarty->assign('commentList', $commentList);// 内容信息
        if ($commentList) {
            $result['error'] = 0;
            $result['content'] = $smarty->fetch("tvd/comtList.lbi");
        } else {
            $result['error'] = 1;
            $result['message'] = '已全部加载！END';
        }
        echo $json->encode($result);
        exit();
    }
    $smarty->assign('act', 'commentList');
    $smarty->display("tvd.dwt");
}
/* 深度垂直内容页信息 */
else {
    assign_template();
    /* 获取来源网址 */
    $sql = "UPDATE " . $ecs->table('tvd') . " SET click = click + 1 WHERE tvd_id = '$id'";
    $db->query($sql);
    $smarty->assign('tvdInfo', getTvdInfo($id));// 内容信息
    $smarty->assign('id', $id);// ID
    /* 随机获取三篇推荐文章 */
    $sql = "SELECT list_img, title, tvd_id FROM egg_tvd WHERE is_open = 1 ORDER BY RAND() LIMIT 3";
    $tvdList = DB()->getAll($sql);
    foreach ($tvdList as $key => $value) {
        $value['url'] = getUrl('tvd', $value['tvd_id']);
        $value['list_img'] = getUrl('tvd_list_img', $value['list_img']);
        $tvdList[$key] = $value;
    }
    $smarty->assign('tvdList', $tvdList);// 推荐
    $smarty->assign('commentList', getTvdComment($id, 0, 3));// 评论
    $smarty->display("tvd/tvd.dwt");
}
